//
//  NewsListTVC.m
//  DayDay
//
//  Created by wade on 17/2/6.
//  Copyright © 2017年 wade. All rights reserved.
//

#import "NewsListTVC.h"
#import "NewsCell.h"
#import "AFNetworking.h"
#import "MJRefresh.h"
#import "UITableView+FDTemplateLayoutCell.h"
@interface NewsListTVC ()
{
    NSMutableArray *dataArray;
    
    NSInteger pageindex;
}
@end

@implementation NewsListTVC

static NSString * const newsCell = @"NewsCell";

- (void)viewDidLoad {
    [super viewDidLoad];
    
    dataArray = [[NSMutableArray alloc] init];
    
    self.tableView.mj_footer = [MJRefreshBackNormalFooter footerWithRefreshingBlock:^{
        
        [self getData];
        
    }];
    
    [self.tableView.mj_footer beginRefreshing];
}

-(void)getData
{
    
    
    AFHTTPSessionManager *manager = [[AFHTTPSessionManager alloc] init];
    manager.responseSerializer = [AFJSONResponseSerializer serializer];
    //manager.requestSerializer = [AFHTTPRequestSerializer serializer];
    [manager.requestSerializer setTimeoutInterval:30];
    //manager.responseSerializer.acceptableContentTypes = [NSSet setWithObject:@"text/html"];
    
    NSMutableDictionary *parameter = [NSMutableDictionary dictionary];
    [parameter setValue:[NSNumber numberWithInteger:pageindex] forKey:@"pageindex"];
    [parameter setValue:[NSNumber numberWithInteger:1] forKey:@"pagesize"];
    
    [manager GET:@"https://newsapi.org/v1/articles?source=the-next-web&sortBy=latest&apiKey=fdb3ebd81d704b8daa537e213cf1680d" parameters:parameter
        progress:^(NSProgress * _Nonnull downloadProgress)
     {
         //NSLog(@"%lld", downloadProgress.totalUnitCount);
     }
         success:^(NSURLSessionDataTask *operation, id responseObject)
     {
         //NSLog(@"%@",responseObject);
         
         //[dataArray addObjectsFromArray:responseObject];
      
         
         dataArray = [responseObject objectForKey:@"articles"];
         
         [self.tableView.mj_footer endRefreshingWithNoMoreData];
         
         
         [self.tableView reloadData];
     }
         failure:^(NSURLSessionDataTask *operation, NSError *error)
     {
         NSLog(@"%@",error);
         
         [self.tableView.mj_footer endRefreshing];
     }];

}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

#pragma mark - Table view data source

-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    return [tableView fd_heightForCellWithIdentifier:newsCell cacheByIndexPath:indexPath configuration:^(NewsCell *cell) {
        
        [cell setNewsCell:[dataArray objectAtIndex:indexPath.section]];
        
    }] ;
}

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
    return [dataArray count];
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return 1;
}

-(CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section
{
    return CGFLOAT_MIN;
}
-(CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section
{
    return CGFLOAT_MIN;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    
    NewsCell *cell = [tableView dequeueReusableCellWithIdentifier:newsCell forIndexPath:indexPath];
    
    [cell setNewsCell:[dataArray objectAtIndex:indexPath.section]];
    
    return cell;
}


- (void) prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender{
    
    if([segue.identifier isEqualToString:@"NewsToDetail"])
    {
        //ProDetailTVC *svc = segue.destinationViewController;
        
        
    }
    
}

@end
